<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Silber\Bouncer\Database\HasRolesAndAbilities;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;


class SyUser extends Authenticatable implements JWTSubject {

    use HasRolesAndAbilities, Notifiable;
   // use \App\Models\Traits\RoleTrait2;
    use \App\Models\Traits\AuthQueryScope;
    //use \App\Models\Traits\Site;
	use \App\Models\Traits\HasUser;

    protected $table = 'sy_user';
    
    protected $primaryKey = 'userid';
    
    protected $fillable = ['userpwd', 'username', 'Co_ID', 'avatar', 'scope', 'site_id'];
    
    protected $hidden = [
        'userpwd', 'remember_token',
    ];
    
    protected $casts = [
        'Co_ID'=>'integer',
        'scope'=>'string',
		'site_id'=>'integer',
    ];
    
    protected  $remember_token;
    

    public function getJWTIdentifier() {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims() {
        return [];
    }

    public function setUserpwdAttribute($value) {
        $this->attributes['userpwd'] = md5($value);
    }

    public function company() {
        return $this->belongsTo('\App\Models\Company', 'Co_ID', 'Co_ID');
    }
    
    public function area() {
        return $this->belongsTo('\App\Models\Area', 'scope', 'AreaId');
    }
    

    public function getAuthPassword() {
        return $this->userpwd;
    }
    
    public function userinfo() {
        return $this->hasOne('\App\Models\Userinfo', 'userid', 'userid');
    }
    
    public function wxuser() {
        return $this->hasOne('\App\Models\WxUser', 'user_id', 'userid');
    }
    
    //超管判断
    public function isSuper() {
       return $this->isA(config('auth.roles.super'));
    }
    
    //管理员
    public function isAdmin() {
       return $this->isA(config('auth.roles.admin'));
    }
    
    
    //管理员
    public function isSuperAdmin() {
       return $this->isA(config('auth.roles.admin'), config('auth.roles.super'));
    }
    
    
    //公司查询
    public function scopeCompany($query, $cid) {
        if($cid) {
            $query->where('Co_ID', $cid);
        }
        return $query;
    }
    
    //站点查询
    public function scopeSite($query, $sid) {
        if($sid) {
            $query->where('scope', $sid);
        }
        return $query;
    }
    
    public function getCompanyField() {
        return 'Co_ID';
    }
    
    public function getSiteField() {
        return 'site_id';
    }

}
